package com.nic.view.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/4/16.
 */

public abstract class CommonAdapter<T> extends BaseAdapter {
    private List<T> tList = new ArrayList<>();

    public void setList(List<T> list) {
        setList(list, true);
    }

    public void setList(List<T> list, boolean isUpDate) {
        if (!this.tList.isEmpty())
            clear();
        if (isNull(list)) return;
        initDataPreview();
        this.tList.addAll(list);
        if (isUpDate) {
            notifyDataSetChanged();
        }
    }

    /**
     * 处理加载数据前内容设置
     */
    public void initDataPreview() {

    }

    public void addAll(List<T> list) {
        addAll(list, true);
    }

    public void addAll(List<T> list, boolean isNotify) {
        if (isNull(list)) return;
        initDataPreview();
        this.tList.addAll(list);
        if (isNotify) {
            notifyDataSetChanged();
        }
    }

    private boolean isNull(List<T> list) {
        if (list == null || list.isEmpty()) {
            return true;
        }
        return false;
    }

    public void addAll(int position, List<T> list) {
        if (isNull(list)) return;
        initDataPreview();
        this.tList.addAll(position, list);
        notifyDataSetChanged();
    }

    public void clear() {
        this.tList.clear();
        notifyDataSetChanged();
    }

    public void add(T e) {
        if (e == null) {
            return;
        }
        initDataPreview();
        this.tList.add(e);
        notifyDataSetChanged();
    }

    public List<T> getList() {
        return tList;
    }

    @Override
    public int getCount() {
        return tList.size();
    }

    @Override
    public Object getItem(int position) {
        return tList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        return getBindView(position, convertView, parent);
    }

    public abstract View getBindView(int position, View convertView, ViewGroup parent);
}
